package cc.iotkit.plugins.mattress.parser;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.vertx.core.buffer.Buffer;
import lombok.extern.slf4j.Slf4j;
import org.msgpack.jackson.dataformat.MessagePackFactory;

import java.io.IOException;

/**
 * 数据解码
 *
 * @author sjg
 */
@Slf4j
public class DataDecoder {

    public static DataPackage decode(Buffer buffer) {
        DataPackage data = new DataPackage();
        // 床垫结果
        ObjectMapper objectMapper = new ObjectMapper(
                new MessagePackFactory());
        try {
            MsgPack msg = objectMapper.readValue(buffer.getBytes(), MsgPack.class);
            data.setAddr(msg.getSn());
            data.setCode(DataPackage.CODE_DATA_UP);
            data.setPayload(buffer.getBytes(0, buffer.length()));
            data.setMsg(msg);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return data;
    }

}
